package com.atguigu.educms.service.impl;

import com.atguigu.educms.entity.BannerQuery;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.mapper.CrmBannerMapper;
import com.atguigu.educms.service.CrmBannerService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author 若chen
 * @since 2022-04-23
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {


    //查询所有banner
    @Cacheable(value = "banner",key = "'selectIndexList'")
    @Override
    public List<CrmBanner> selectAllBanner() {

        //根据id进行降序排列，显示排列之后前两条记录
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //last方法，拼接sql语句
        wrapper.last("limit 2");
        List<CrmBanner> list = baseMapper.selectList(null);
        return list;
    }


    //添加轮播图
    @Override
    public void saveBanner(BannerQuery bannerQuery) {
        CrmBanner banner = new CrmBanner();
        BeanUtils.copyProperties(bannerQuery, banner);
        int insert = baseMapper.insert(banner);
        if (insert == 0) {
            throw new GuliException(444, "添加轮播信息失败异常");
        }
    }

    //修改轮播图
    @Override
    public void updateBannerById(BannerQuery banner) {
        CrmBanner crmBanner = new CrmBanner();
        BeanUtils.copyProperties(banner, crmBanner);
        int update = baseMapper.updateById(crmBanner);
        if (update == 0) {
            throw new GuliException(444, "修改轮播信息失败异常");
        }
    }

    @Override
    public List<CrmBanner> findAllBanner() {
        return baseMapper.selectList(null);
    }

}
